var path = require("path");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HtmlPlugin = require('html-webpack-plugin');
const ChromeExtensionReloader  = require('webpack-chrome-extension-reloader');
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin')
const utils =require('./utils/index')
const pkg = require('../package.json');
module.exports = {
    mode: 'development',//开发欧式
    watch: true,//配合开发模式使用，时刻监听文件变化，然后打包到指定位置
    watchOptions: {
        aggregateTimeout: 300,
        poll: 1000,
        ignored: utils.resolve('/node_modules'),//忽略文件
    },
    node: {
        fs: 'empty'
    },
    entry: {
        popup: ["./src/popup/index.tsx"],  //入口文件,
        // option: ["./src/index.tsx"],  //选择入口文件,
        background: ["./src/background/index.tsx"],  //常驻后台入口文件,
        // vendor: Object.keys(pkg.dependencies),//把依赖文件打包进一个文件vendor里面
    },
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src')
        },
        extensions: ['*', '.tsx', '.ts', '.js', '.jsx', '.json']//忽略文件名
    },
    output: {
        path: path.resolve(__dirname + "/../", "dist"), //出口目录，绝对路径
        filename: 'js/[name].js',
        // chunkFilename: path.resolve(__dirname + "/../", "dist")
        //filename: "bundle.js"  //出口文件名字 也可以使用 filename:"[name].js"，会根据entry下面的key值进行文件名命名
    },
    plugins: [
        new ChromeExtensionReloader(),
        utils.getPage({
            name: 'popup',
            chunks: ['popup'],
            template: './public/index.html'
        }),
        // utils.getPage({
        //     name: 'option',
        //     chunks: ['option'],
        //     template: './public/index.html'
        // }),
        utils.getPage({
            name: 'background',
            chunks: ['background'],
            template: './public/index.html'
        }),
        // new UglifyJsPlugin({
        //     test: /\.js($|\?)/i,
        // }),
        // new CleanWebpackPlugin({
        //     cleanAfterEveryBuildPatterns: ['dist']
        // }),
        new CopyWebpackPlugin({
            patterns:[
                { 
                    from: utils.resolve('/src/*.json'),
                    to: utils.resolve('/dist/[name].json')//静态资源copy
                },
                { 
                    from: utils.resolve('/public'),
                    to: utils.resolve('/dist')//静态资源copy
                }
            ],
           
        }),
    ],
    module: {
        rules: [
            {
                test: /\.less|\.css$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },
            {
                test: /\.(png|jpg|gif|ttf|eot|svg|woff|woff2)$/,
                exclude: ['/node_modules/','/dist/'],
                use: [
                  {
                    loader: 'file-loader',
                    options: {}
                  }
                ]
            },
            { 
                test: /\.tsx?$/, 
                loader: 'ts-loader',
                options: {
                    configFile: utils.resolve('/config/tsconfig.json')
        　　　　　}
            }
        ]
    },
    //devtool: 'source-map'  这里主要是用以开启map  定位打包出来的文件和源文件不一样的问题
}

//